package com.wondertek.poms.core.service.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.common.utils.CopyUtils;
import com.wondertek.poms.core.service.ISyncTaskService;
import com.wondertek.poms.dao.po.SyncTask;
import com.wondertek.poms.dao.repo.ISyncTaskDao;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;


/**
 * <p><b>SyncTaskServiceImpl Description:</b>  service层实现 </p>
 * @author 窦再兴
 * <p><b>DATE</b> 2020-10-21 13:50:43</p>
 */
@Service
@Scope("prototype")
public class SyncTaskServiceImpl extends ServiceImpl<SyncTask, Long> implements ISyncTaskService {

	@Autowired
	private ISyncTaskDao syncTaskDao;
	@PersistenceContext
    public EntityManager entityManager;

	@Override
	@Transactional
	public SyncTask deleteLogic(Long id) {
		SyncTask cp = jpaRepository.getOne(id);
		jpaRepository.save(cp);
		return cp;
	}

	@Override
	@Transactional
	public SyncTask merge(SyncTask model) {
		SyncTask dbo = jpaRepository.getOne(model.getId());
		CopyUtils.copyProperties(model, dbo);
		return jpaRepository.save(dbo);
	}

	@Override
	public SyncTask getLastOfflineSyncTaskByContId(String contentId) {
		List<SyncTask> list=syncTaskDao.getLastOfflineSyncTaskByContId(contentId);
		if(list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public Page<SyncTask> findAll(SyncTask entity, int page, int rows, String sord, String sidx) {
		StringBuffer condition=sqlStringWhere(entity,sord,sidx);
		Object o = entityManager
				.createNativeQuery("select count(*) from ( " + condition.toString() + " ) a").getSingleResult();
		int count = Convert.toInt(o);
		Pageable pageable = getPageable(page - 1, rows, sord, sidx);

		if(count>0){
			Query query=entityManager.createNativeQuery(condition.toString(),SyncTask.class);
			query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
			query.setMaxResults(pageable.getPageSize());
			List<SyncTask> list=query.getResultList();
			PageImpl<SyncTask> result=new PageImpl<>(list,pageable,count);
			entityManager.close();
			return result;
		}
		entityManager.close();
		return Page.empty(pageable);
	}

	public StringBuffer sqlStringWhere(SyncTask entity, String sord, String sidx){
		StringBuffer condition=new StringBuffer();
		String sql="select * from SYNC_TASK where 1=1 ";
		condition.append(sql);

		if(StrUtil.isNotBlank(entity.getContentId())){
			condition.append("and CONTENT_ID= '"+entity.getContentId()+"' ");
		}
		if(StrUtil.isNotBlank(entity.getAccessPlatformType()) && !"0".equals(entity.getAccessPlatformType())){
			condition.append("and ACCESS_PLATFORM_TYPE= '"+entity.getAccessPlatformType()+"' ");
		}
		if(StrUtil.isNotBlank(entity.getFromDate())){
			condition.append("and SYNC_TIME >= to_date('"+entity.getFromDate()+"','yyyy-mm-dd hh24:mi:ss') ");
		}
		if(StrUtil.isNotBlank(entity.getToDate())){
			condition.append("and SYNC_TIME <= to_date('"+entity.getToDate()+"','yyyy-mm-dd hh24:mi:ss') ");
		}
		if(entity.getStatus() != null && entity.getStatus() != 0){
			condition.append("and STATUS = "+entity.getStatus()+" ");
		}
		condition.append("order by "+sidx+" "+sord);
		return condition;
	}
}